package org.example.flea_market_mvc.service.user.impl;

import org.example.flea_market_mvc.entity.User;
import org.example.flea_market_mvc.mapper.UserMapper;
import org.example.flea_market_mvc.param.LoginParam;
import org.example.flea_market_mvc.param.UserParam;
import org.example.flea_market_mvc.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务实现类
 * 实现用户相关的业务逻辑，调用UserMapper完成数据库操作，包括登录、注册等
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(UserParam userParam) {
        // 将参数对象转换为实体对象
        User user = new User();
        BeanUtils.copyProperties(userParam, user);
        // 调用Mapper插入数据库
        return userMapper.insert(user);
    }

    @Override
    public int deleteById(Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int update(User userParam) {

        return userMapper.update(userParam);
    }

    @Override
    public User selectById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User login(UserParam loginParam) {
        // 这里简单假设可以根据用户名查询用户
        List<User> users = userMapper.selectAll();
        for (User user : users) {
            if (user.getUsername().equals(loginParam.getUsername()) && user.getPassword().equals(loginParam.getPassword())) {
                return user;
            }
        }
        return null;
    }
}